﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace QuickExcel
{
    public partial class sortSheets : Form
    {        
        public sortSheets()
        {
            InitializeComponent();
            Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;

            foreach (Excel.Worksheet wc in wb.Sheets)
            {
                listSheets.Items.Add(wc.Name.ToString());
            }

            listSheets.SelectedIndex = 0;
        }

        private void cmdMoveUp_Click(object sender, EventArgs e)
        {
            string sTempVal;
            listSheets.Sorted = false;
            sTempVal = Convert.ToString(listSheets.SelectedItem);            
            listSheets.Items[listSheets.SelectedIndex] = listSheets.Items[listSheets.SelectedIndex - 1];
            listSheets.Items[listSheets.SelectedIndex - 1] = sTempVal;
            listSheets.SelectedIndex = listSheets.SelectedIndex - 1;
        }

        private void cmdMoveDown_Click(object sender, EventArgs e)
        {
            string sTempVal;
            listSheets.Sorted = false;
            sTempVal = Convert.ToString(listSheets.SelectedItem);

            listSheets.Items[listSheets.SelectedIndex] = listSheets.Items[listSheets.SelectedIndex + 1];
            listSheets.Items[listSheets.SelectedIndex + 1] = sTempVal;
            listSheets.SelectedIndex = listSheets.SelectedIndex + 1;
        }

        private void listSheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmdMoveDown.Enabled = cmdMoveUp.Enabled = true;
            
            if (listSheets.SelectedIndex == 0)
                cmdMoveUp.Enabled = false;
            else if (listSheets.SelectedIndex == listSheets.Items.Count-1)
                cmdMoveDown.Enabled = false;
        }

        private void cmdAlphaSort_Click(object sender, EventArgs e)
        {
            object[] arr = new object[listSheets.Items.Count];
            int index = 0;
            int start, end;

            listSheets.Items.CopyTo(arr,index);
            Array.Sort(arr);

            start = chkReverse.Checked ? arr.Length-1 : 0;
            end = chkReverse.Checked ? -1 : arr.Length;

            listSheets.Items.Clear();

            for (index = start; index!= end; index = chkReverse.Checked ? index-1 : index+1)            
                listSheets.Items.Add((string)arr[index]);

            Array.Clear(arr,0,arr.Length);
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            Globals.ThisAddIn.Application.ScreenUpdating = false;
            for(int i=listSheets.Items.Count-1;i>=0;i--)
            {
                Globals.ThisAddIn.Application.ActiveWorkbook.Sheets[listSheets.Items[i]].Move(Globals.ThisAddIn.Application.ActiveWorkbook.Sheets[1]);
            }
            Globals.ThisAddIn.Application.ScreenUpdating = true;
            this.Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
